import re
from zoot import regex

multiline_re = re.compile(r'\r?\n(?:[ \t]*\r?\n)+')


#-------------------------------------------------------------------------------
def text_to_html(text):
    text = regex.url.sub(r'<a href="\g<0>">\g<0></a>', text)
    lines = []
    for para in multiline_re.split(text):
        lines.append('<p>')
        lines.append('\n    '.join(para.splitlines()))
        lines.append('</p>')
        
    return '\n'.join(lines)

try:
    from docutils.core import publish_parts
    
except ImportError:
    render_rst = None

else:
    #---------------------------------------------------------------------------
    def render_rst(source):
        parts = publish_parts(source=source, writer_name='html4css1')
        return parts['html_body']
    
